var OpenIDProviders = {

	openid: {
        name: 'OpenID',     
        label: 'Введите Ваш OpenID.',
        url: 'http://'
    },
	yandex: {
        name: 'Yandex',     
        url: 'http://yandex.ru'
    },
    google: {
        name: 'Google',
        url: 'https://www.google.com/accounts/o8/id'
    },
    yahoo: {
        name: 'Yahoo',      
        url: 'http://yahoo.com/'
    },
    
    
    myopenid: {
        name: 'MyOpenID',
        label: 'Ваш MyOpenID логин',
        url: 'http://{username}.myopenid.com/'
    },
    livejournal: {
        name: 'LiveJournal',
        label: 'Ваш Livejournal логин',
        url: 'http://{username}.livejournal.com/'
    },
    technorati: {
        name: 'Technorati',
        label: 'Ваш Technorati логин',
        url: 'http://technorati.com/people/technorati/{username}/'
    },
    wordpress: {
        name: 'Wordpress',
        label: 'Ваш Wordpress.com логин',
        url: 'http://{username}.wordpress.com/'
    },
    verisign: {
        name: 'Verisign',
        label: 'Ваш Verisign логин',
        url: 'http://{username}.pip.verisignlabs.com/'
    },
    vidoop: {
        name: 'Vidoop',
        label: 'Ваш Vidoop логин',
        url: 'http://{username}.myvidoop.com/'
    },
    verisign: {
        name: 'Verisign',
        label: 'Ваш Verisign логин',
        url: 'http://{username}.pip.verisignlabs.com/'
    },
    claimid: {
        name: 'ClaimID',
        label: 'Ваш ClaimID логин',
        url: 'http://claimid.com/{username}'
    },
    aol: {
        name: 'AOL',     
        label: 'Введите Ваш AOL логин',
        url: 'http://openid.aol.com/{username}'
    }    

};

var OpenIDProvidersMini = {openid : OpenIDProviders.openid,yandex:OpenIDProviders.yandex,google:OpenIDProviders.google,yahoo:OpenIDProviders.yahoo, myopenid:OpenIDProviders.myopenid, livejournal:OpenIDProviders.livejournal};


var OpenIDSelector = Class.create({
 initialize : function(init){
 	this.placeholder = init.placeholder;
 	this.providers = $H(init.providers);
 	this.identifierName = init.identifierName;
 	this.formName = init.formName;
 	this.formAction = init.formAction;
 	this.initUI();
 },
 
 initUI : function(){
   this.form = new Element('form', {'class':'openidSelector', 'name':this.formName, 'action':this.formAction});
   this.hiddenIdentifier = new Element('input', {'type':'hidden', 'name':this.identifierName});
   this.form.appendChild(this.hiddenIdentifier);
   this.form.observe('submit',this.setURL.bind(this));
   this.buttonsDiv = new Element('div', {'class':'buttons'});
   this.form.appendChild(this.buttonsDiv);
   this.inputArea = new Element('div', {'class':'inputArea'});
   this.form.appendChild(this.inputArea);
   this.inputLabel = new Element('div', {'class':'inputLabel'}).update('Введите URL авторизации');
   this.input = new Element('input', {'type':'text', 'value':'http://', 'class':'identifier'});
   this.submit = new Element('input', {'type':'submit', 'value':'Войти'});
   this.inputArea.appendChild(this.inputLabel);
   this.inputArea.appendChild(this.input);
   this.inputArea.appendChild(this.submit);
   this.placeholder.appendChild(this.form);
   this.providers.each(function(provider){
         var box = new Element('a',{'href':'javascript:void(0)','class':provider.key});
	     box.observe('click',this.signIn.bindAsEventListener(this,provider.value,box));
	     this.buttonsDiv.appendChild(box);
    }.bind(this)); 
 },
 
 setURL:function(){
 	this.submit.disable();
 	this.hiddenIdentifier.value = this.selectedProvider.url.replace('{username}', this.input.value);
 
 },
 
 signIn : function(event, provider,box){
    this.selectedProvider = provider;
    this.buttonsDiv.select('a').each(function(el){el.removeClassName('selected');});
    box.addClassName('selected');
	this.inputLabel.update(provider.label?provider.label:"");
    this.input.value = provider.url.include('{username}')?"":provider.url;
    this.input.focus();
    if(!provider.label){
        this.setURL();
    	this.form.submit();
    }
 }
});

